package com.explam.springboot.controller.user;

import cn.hutool.core.util.StrUtil;
import com.explam.springboot.common.ResultUtil;
import com.explam.springboot.entity.User;
import com.explam.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/app/user")
public class UserAppController {

    @Autowired
    private IUserService userService;

    /*
    * 用户登录
    * */
    @PostMapping("/login")
    private ResultUtil userAppLogin(@RequestBody User user){
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            return ResultUtil.error("账号密码不能为空");
        }
        User useredAppLogin = userService.userAppLogin(user);
        return ResultUtil.success("登录成功", useredAppLogin);
    }

    /*
     * 用户注册
     * */
    @PostMapping("/register")
    private ResultUtil userAppRegister(@RequestBody User user){
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            return ResultUtil.error("账号密码不能为空");
        }
        Integer i = userService.userAppRegister(user);
        if(i != 1){
            return ResultUtil.error("注册失败");
        }
        return ResultUtil.success("注册成功", i);
    }

    /*
    * 用户修改信息
    * */
    @PostMapping("/update-info")
    private ResultUtil updateUserInfo(@RequestBody User user){
        userService.saveOrUpdate(user);
        return ResultUtil.success("修改成功");
    }

    /*
    * 根据id查询个人信息
    * */
    @GetMapping("/user-info/id={id}")
    public ResultUtil getUserById(@PathVariable Integer id){
        ResultUtil resultUtil = new ResultUtil();
        User user = userService.getById(id);
        return ResultUtil.success("success", user);
    }
}
